﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Cryptography;

namespace Com.CMS263.Web.manage
{
    public partial class manage_email : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Post 数据至接口端函数
        /// </summary>
        /// <param name="postContent">提交内容</param>
        /// <param name="url">提交地址</param>
        /// <returns></returns>
        private string PostHttp(string postContent, string url)
        {
            string resultstr = string.Empty;
            System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "text/xml";
            req.Timeout = 1000000;
            Byte[] bytes = encode.GetBytes(postContent);
            req.ContentLength = bytes.Length;
            Stream sendStream = req.GetRequestStream();
            sendStream.Write(bytes, 0, bytes.Length);
            sendStream.Close();
            System.Net.WebResponse rep = req.GetResponse();
            Stream getStream = rep.GetResponseStream();
            using (StreamReader sr = new StreamReader(getStream, Encoding.UTF8))
            {
                resultstr = sr.ReadToEnd();
                sr.Close();
            }
            getStream.Close();
            rep.Close();
            return resultstr;
        }

        ///发送短信方法
        private string sendSms(object sender, EventArgs e)
        {
            //构建所需Post的参数            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<Group Login_Name=""{0}"" Login_Pwd=""{1}"" OpKind=""0"" InterFaceID=""2"">", "username", EncryptTomd5("userpwd"));
            sb.AppendFormat("<E_Time>{0}</E_Time>", "");
            sb.AppendFormat("<Item>");
            sb.AppendFormat("<Task><Recive_Phone_Number>{0}</Recive_Phone_Number>", "15010322196");
            sb.AppendFormat("<Content><![CDATA[{0}]]></Content>", "现在可以了，原来是Oracle的M");
            sb.AppendFormat("<Search_ID>{0}</Search_ID>", "1453265");
            sb.Append("</Task>");
            sb.Append("</Item></Group>");

            return PostHttp(sb.ToString(), "http://slapi.vcomcn.com/Opration.aspx");
        }


        //用MD5方式加密字符串函数
        private string EncryptTomd5(string strPassword)
        {
            ASCIIEncoding objAsc = new ASCIIEncoding();
            MD5 objMD5 = new MD5CryptoServiceProvider();
            byte[] arrRndHashPwd = objMD5.ComputeHash(objAsc.GetBytes(strPassword));
            string strRndHashPwd = "";
            foreach (byte b in arrRndHashPwd)
            {
                if (b < 16)
                    strRndHashPwd = strRndHashPwd + "0" + b.ToString("X");
                else
                    strRndHashPwd = strRndHashPwd + b.ToString("X");
            }
            return strRndHashPwd;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            sendSms(sender, e);
        }
        
    }
}
